package com.grow.greenhouse.controller;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.grow.greenhouse.common.BaseResponse;
import com.grow.greenhouse.common.ErrorCode;
import com.grow.greenhouse.domain.Equipdailynum;
import com.grow.greenhouse.domain.ExceptionVO;
import com.grow.greenhouse.domain.Historyexception;
import com.grow.greenhouse.domain.User;
import com.grow.greenhouse.domain.request.DailyNumRequest;
import com.grow.greenhouse.domain.request.ShedNameRequest;
import com.grow.greenhouse.exception.BusinessException;
import com.grow.greenhouse.service.EquipdailynumService;
import com.grow.greenhouse.service.HistoryexceptionService;
import com.grow.greenhouse.service.ShedService;
import com.grow.greenhouse.service.UserService;
import com.grow.greenhouse.utils.ResultUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 主页请求数据
 */
@RestController
@Slf4j
@RequestMapping("/index")
public class IndexController {

    @Resource
    private UserService userService;

    @Resource
    private ShedService shedService;

    @Resource
    private HistoryexceptionService historyexceptionService;

    @Resource
    private EquipdailynumService equipdailynumService;


    @Resource
    private Gson gson;

    @ApiOperation("查看当前用户拥有的所有大棚")
    @GetMapping("/shed")
    public BaseResponse<List<String>> getUserShed(HttpServletRequest request)
    {
        User user = userService.getLoginUser(request);
        List<String> shedList = gson.fromJson(user.getShedName(), new TypeToken<List<String>>() {
        }.getType());
        return ResultUtils.success(shedList);
    }

    @ApiOperation("用户选择大棚")
    @PostMapping("/choose/shed")
    public BaseResponse<String> chooseShed(@RequestBody ShedNameRequest shedName
            , HttpServletRequest request)
    {

        if(shedName == null){
            return null;
        }
        if(StringUtils.isBlank(shedName.getShedName())){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"棚名称不可为空");
        }
        String chooseShed = shedService.chooseShed(shedName.getShedName(), request);
        return ResultUtils.success(chooseShed);

    }

    @ApiOperation("用户查看异常（用于主页展示，仅三条）")
    @GetMapping("/exception")
    public BaseResponse<List<ExceptionVO>> getExceptionInfo(HttpServletRequest request)
    {
        List<ExceptionVO> exceptions = historyexceptionService.getException(request);
        return ResultUtils.success(exceptions);
    }

    @ApiOperation("用户查看异常，可以查看所有异常信息")
    @GetMapping("/exception/all")
    public BaseResponse<List<ExceptionVO>> getAllExceptionInfo(HttpServletRequest request)
    {
        List<ExceptionVO> exceptions = historyexceptionService.getAllException(request);
        return ResultUtils.success(exceptions);
    }

    @ApiOperation("数据分析页展示改指标的当日数据")
    @PostMapping("/daily")
    public BaseResponse<List<Double>> displayDailyNum(@RequestBody DailyNumRequest dailyNumRequest
            , HttpServletRequest request)
    {
        if(dailyNumRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"传递参数不能为空");
        }
        List<Double> equipdailynums = equipdailynumService.showDailyNum(dailyNumRequest,request);
        return ResultUtils.success(equipdailynums);
    }



//    @PostMapping("/choose/shed")
//    public BaseResponse<String> chooseShed(@RequestBody String shedName, HttpServletRequest request){
//        String token = JwtUtil.getUserInfoFromToken(request.getHeader("token"));
//        Map<String,String> jsonMap = gson.fromJson(token, new TypeToken<Map<String, String>>() {
//        }.getType());
//        long userId = Long.parseLong(jsonMap.get("userId"));
//        if(userId <= 0){
//            throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户id应为正数");
//        }
//        User user = userService.getById(userId);
//        if(user == null){
//            throw new BusinessException(ErrorCode.NOT_LOGIN,"用户不存在，请检查是否登录");
//        }
//        UserContext.setShedName(shedName);
//        String returnToken = JwtUtil.createToken(userId);
//        return ResultUtils.success(returnToken);
//    }


//    @PostMapping("/home")
//    public BaseResponse<Boolean> transportIndexMsgByWebSocket(HttpServletRequest request){
//
//    }

}
